<?php
namespace Home\Controller;
use Common\Controller\Addon;
use OT\DataDictionary;
use Think\Page;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController {
	
	static public $SchoolRelation;

	//系统首页
    public function index(){
    	$SchoolType=M('SchoolType');
    	$SchoolType_data=$SchoolType->select();
    	$m=M();
    	//查询每个省的学校数量
    	$sql="SELECT\n".
			 "	COUNT(school_id) as school_number , school_type_name ,onethink_school_type.school_type_id,school_type_sort\n".
			 "FROM\n".
			 "	onethink_school_relation\n".
			 "LEFT JOIN onethink_school_type ON onethink_school_relation.school_type_id=onethink_school_type.school_type_id\n".
			 "WHERE\n".
			 "	school_type_sort=2\n".
			 "GROUP BY school_type_name;";
    	$m_data=$m->query($sql);
    	
   		//根据条件获得学校列表
   		$this->schoolList();
   		$factor=I('param.');
               
		unset($factor['order']);
		
   		if(!$_GET['p'])
   		$_SESSION['map_type']=$factor;
    	
   		if($_GET['p'])
                $factor=$_SESSION['map_type'];
                

    	$factor_one_array=$this->factor_one_array($factor);
  		
   		//热门排行
   		$school=M('school');
		$school_desc=$school->order('order_id desc')->limit(25)->select();
		
		$this->assign('school_desc',$school_desc);
    	
		$this->assign('factor_one_array',$factor_one_array);
        $this->assign('SchoolType_data',$SchoolType_data);
        $this->assign('m',$m_data); 
       	$this->assign('factor',$factor);
        $this->display();
    }
    
   	/**
   	 * 二维数组降维
   	 * @param unknown_type $factor
   	 * @return unknown
   	 */
   	public function factor_one_array($factor=null)
   	{	
   		foreach($factor as $v)
       	{
       		if(is_array($v))
       		{
       			foreach ($v as $v1)
       			{
       				$arr[]=$v1;
       			
       			}
       		
       		}
       		else
       		{
       			$arr[]=$v;
       		
       		}
       	
       	}
        
   		return $arr;
   	
   	}  
    
    /**
     * 根据条件获取学校列表
     * @return school_list
     */
    public  function schoolList()
    {	
    	$map_type=I('param.');
    	if($_GET['p'])
                $map_type=$_SESSION['map_type'];
    	if(!$_GET['p'])
                $_SESSION['map_type']=$map_type;
        
        //新添加一个模糊查询 根据学校名称模糊查询
        //由于前面的操作都是SchoolRelation 所以把$map_type['school_name']抽离出来以免影响其他的条件查询 赋值给$school_name
	$school_name=$map_type['school_name'];
        //释放$map_type['school_name'] 以免影响其他
        unset($map_type['school_name']);
    	$this->SchoolRelation=M('SchoolRelation');
    	//$this->assign('url',$url);
  		
    	 
    	if(!$map_type['school_type'])$map_type['school_type']='1';
    	
    	//根据条件查询院校类型的学校ID
    	if($map_type['school_type'])
    	{	
    		
    		$map['school_type_id']=array('in',$map_type['school_type']);
    		

    		$map_data=$this->query($map);
    	
    	}
    	//根据结果查询评分
    	unset($map);
    	if($map_type['school_ratings'])
    	{
    		$School=M('School');
    		
    		$this->assign('school_ratings',$map_type['school_ratings']);
    		$map['school_ratings']=array('between',$map_type['school_ratings']);
    		$map['school_id']=array('in',$map_data);
    		$map_data=$School->where($map)->getField('school_id',true);
    		unset($map_type['school_ratings']);
    		
    	}
    
    	//遍历筛选条件
    	$map_type_count=count($map_type);
  		if($map_type_count>1)
  		{ 
  			foreach ($map_type as $k=>$v)
  			{
  				
  				if($map_data)
  				$map_data=$this->judge_school_id($v, $map_data);
  				
  			}
  			
  		}
    	
  		//查询最后结果并输出
  		unset($map);
  		$map['school_id']=array('in',$map_data);
                //判断模糊条件是否为空
  		if(!empty($school_name))
                {
                    $map['school_name'] = array('like','%'.$school_name.'%');
                }
  		$this->page($map,$map_type,$url);
 		

    }
    
    /**
     * 组合学校搜索条件
     * @param unknown_type $map_type
     * @param unknown_type $map_data
     * @return Ambigous <\Home\Controller\multitype:, multitype:>|boolean
     */
    public function judge_school_id($map_type='1',$map_data=null)
    {
    	if($map_data)
  		{
  			$map['school_id']=array('in',$map_data);
  			$map['school_type_id']=array('in',$map_type);
  			$map_data=$this->query($map);
  			return $map_data;
  		}
  		return false;
    
    }
 
    /**
     * 根据条件查询school_id
     * @param unknown_type $map
     * @return multitype:
     */
    protected  function query($map)
    {   	
    	$SchoolRelation_data=$this->SchoolRelation->where($map)->getField('school_id',true);
    	$SchoolRelation_data=array_unique($SchoolRelation_data);
    	return $SchoolRelation_data;
    }
    
    

	/**
	 * 数据分页
	 * @param unknown_type $map
	 */
	protected  function page($map,$map_type,$url)
	{	

     if(I("post.order") == 2){
                    $order="school_ratings desc";
                    $this->assign('order',2);
   		 }elseif(I("post.order") == 3){
                    $order="comment desc";
                    $this->assign('order',3);
         }else{
                    $order="";
                    $this->assign('order',1);
         }
         
		$school = D('School'); // 实例化User对象
		$count      = $school->relation(true)->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$Page->setConfig('prev', '  &laquo;');
		$Page->setConfig('next', '&raquo;');
		//$Page->setConfig('current',' <li class="actived"><a href="#">1</a></li>');
		//$Page->parameter=$map_type;
		$show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$School_data = $school->relation(true)->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('page',$show);// 
		$this->assign('school_number',$count);
		$this->assign('school_data',$School_data);// 赋值数据集
		
	}
	
	
	
}